<?php
namespace App\Model;
use PhalApi\Model\NotORMModel as NotORM;
use App\Model\Agent as Model_Agent;

class Charge extends NotORM {
	/* 充值规则列表 */
	public function getChargeRules($uid='') {
		$user = \PhalApi\DI()->notorm->user
				->select('id,country')
				->where('id',$uid)
				->fetchOne();
		$country = \PhalApi\DI()->notorm->country
				->select('id,code,unit,mark')
				->where('code',$user['country'])
				->order('id asc')
				->fetchOne();
		$rules=\PhalApi\DI()->notorm->charge_rules
				->select('id,name,name_en,money,coin,coin_ios,coin_paypal,product_id,give')
				->order('coin asc')
				->fetchAll();
        if(!empty($rules)){
            foreach ($rules as $k=>$v){
                $money = json_decode($v['money'],true);
                foreach ($money as $k1=>$v1){
                    if(isset($country['code']) && $country['code'] == $k1){
                        $country['num'] = $v1;
                    }else{
                        if($k1 == 'US'){
                            $country['num'] = $v1;
                            $country['unit'] = 'USD';
                            $country['mark'] = '$';
                        }
                    }
                }
                $rules[$k]['money'] = $country;
            }
        }
        //print_r($rules);exit;
		return $rules;
	}
    
    /* 获取充值规则 */
	public function getChargeRule($changeid) {
		
		$charge=\PhalApi\DI()->notorm->charge_rules
			->select('*')
			->where('id=?',$changeid)
			->fetchOne();

		return $charge;
	}		

	/* 订单号 */
	public function setOrder($orderinfo) {

		$result= \PhalApi\DI()->notorm->charge_user->insert($orderinfo);

		return $result;
	}

	/* 订单号 */
	public function updatePaymentStatus($uid,$nonce,$orderid,$money) {
			 
		$orderinfo=\PhalApi\DI()->notorm->charge_user
			->where('orderno=? and money=? ',$orderid,$money)
			->fetchOne();
		
		if(!$orderinfo){
			return 1003;
		}
		
		if($orderinfo['status']!=0){
			return 0;
		}
	
		/* 更新会员虚拟币 */
        $coin=$orderinfo['coin']+$orderinfo['coin_give'];
        /* 更新用户余额 消费 */
		\PhalApi\DI()->notorm->user
				->where('id = ?', $uid)
				->update( array('coin' => new \NotORM_Literal("coin + {$coin}")));
        /* 更新 订单状态 */
       
        $data['status']=1;
		//$data['nonce']=$nonce;
		$data['trade_no']=$nonce;
		$configpri=\App\getConfigPri();
		if($configpri['paypal_pattern_braintree']==0){
			$data['ambient']=0;
		}else{
			$data['ambient']=1;
		}
		
		
        \PhalApi\DI()->notorm->charge_user->where("orderno=? ",$orderid)->update($data);
       
        
        $res=$this->setAgentProfit($uid,$orderinfo['coin']);
				
		return $res;
	}

	/* 邀请奖励 */
    function setAgentProfit($uid,$total){
        if($uid<1 || $total<=0){
            return !1;
        }
        $info=\PhalApi\DI()->notorm->agent->where("uid=? ",$uid)->fetchOne();
        if($info){
            $configpri=getConfigPri();
            $agent_one=$configpri['agent_one'];
            
            $one=$info['one'];
            
            $profit_one=0;

            $model_agent= new Model_Agent();
            
            if($one>0 && $agent_one>0){
                $profit_one=floor($total*$agent_one*0.01);
                if($profit_one>0){

                    $isexist=$model_agent->getProfit($one);
                    if(!$isexist){

                        $data=[
                            'uid'=>$one,
                            'one_p'=>$profit_one,
                        ];

                        $model_agent->setProfit($data);

                    }else{

                    	$model_agent->upProfit($one,'one_p',$profit_one);

                    }
                    
                    \App\addVotes($one,$profit_one,$profit_one);

                    //增加映票记录
                    $data=array(
                    	'type'=>1,
                    	'action'=>2, //下级充值上级分成奖励
                    	'uid'=>$one,
                    	'fromid'=>$uid,
                    	'total'=>$profit_one,
                    	'votes'=>$profit_one,
                    	'addtime'=>time()
                    );

                    \App\addVoteRecord($data);

                }
            }

            if(!$profit_one){
                return !1;
            }

            $isexist=$model_agent->getProfit($uid);

            if(!$isexist){

                $data=[
                    'uid'=>$uid,
                    'one'=>$profit_one,
                ];

               $model_agent->setProfit($data);

            }else{
            	$model_agent->upContri($uid,$profit_one);
            }
        }
		return 0;
    }	

}
